<script setup>
import { getTreeComments } from '@/utils/data'
import { ref } from 'vue'
import CommentItem from '@/components/blog-components/comment-item.vue'

const props = defineProps({
    comments: {
        type: Array,
        default: () => []
    }
})
const replyContent = ref('')
// 计算总评论数
const getTotalComments = (commentList) => {
    let count = 0
    const countComments = (items) => {
        items.forEach(item => {
            count++
            if (item.children && item.children.length) {
                countComments(item.children)
            }
        })
    }
    countComments(commentList)
    return count
}
const submitReply = () => {
    if (!replyContent.value.trim()) {
        alert('请输入回复内容')
        return
    }
    comments.value.push({
        id: generateId(),
        label: `用户${Date.now().toString().slice(-4)}`, // 模拟用户名
        content: replyContent.value.trim(),
        time: new Date().toLocaleString(),
        children: []
    })

    replyContent.value = ''
}
// 处理回复
const handleReply = (replyData) => {
    console.log('收到回复数据:', replyData)

    // 在实际项目中，这里应该调用API提交回复
    // 这里模拟添加回复到评论树中
    addReplyToComment(comments.value, replyData.targetCommentId, {
        id: generateId(),
        label: `用户${Date.now().toString().slice(-4)}`, // 模拟用户名
        content: replyData.content,
        time: new Date().toLocaleString(),
        children: []
    })
}

// 递归查找评论并添加回复
const addReplyToComment = (commentList, targetId, newReply) => {
    for (let comment of commentList) {
        if (comment.id === targetId) {
            if (!comment.children) {
                comment.children = []
            }
            comment.children.push(newReply)
            return true
        }
        if (comment.children && comment.children.length) {
            const found = addReplyToComment(comment.children, targetId, newReply)
            if (found) return true
        }
    }
    return false
}

// 生成唯一ID
const generateId = () => {
    return `reply-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`
}
</script>

<template>
    <div class="comments-area">
        <div class="comments-header">
            <h2>评论</h2>
            <span class="comments-count">{{ getTotalComments(comments) }}条</span>
        </div>

        <div class="comments-list">
            <CommentItem v-for="comment in comments" :key="comment.id" :comment="comment" :level="0"
                @reply="handleReply" />
        </div>
    </div>
    <div class="reply-input">
        <!-- 在输入框中显示回复对象 -->
        <textarea ref="replyTextarea" v-model="replyContent" placeholder="写下你的回复..." rows="3"
            class="reply-textarea"></textarea>
        <div class="reply-actions">
            <button class="submit-btn" @click="submitReply">回复</button>
        </div>
    </div>
</template>

<style lang="scss" scoped>
.comments-area {
    border-top: 1px solid #eee;
    margin: 0 auto;
    width: 100%;
    padding: 16px;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);

    .comments-header {
        display: flex;
        align-items: center;
        margin-bottom: 16px;

        h2 {
            margin: 0;
            font-size: 18px;
        }

        .comments-count {
            margin-left: 8px;
            font-size: 14px;
        }
    }

    .comments-list {
        margin-bottom: 16px;
    }
}

/* 回复输入框样式 */
.reply-input {
    margin-top: 12px;
    padding: 12px;
    border-radius: 8px;
    border: 1px solid #e9ecef;
}

.reply-textarea {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--text-color);
    background: transparent;
    border-radius: 4px;
    font-size: 14px;
    line-height: 1.5;
    resize: vertical;
    font-family: inherit;

    &:focus {
        outline: none;
        border-color: #1890ff;
        box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.1);
    }

    &::placeholder {
        color: #999;
    }
}

.reply-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 8px;
}

.submit-btn {
    padding: 6px 12px;
    border: 1px solid #1890ff;
    background: #1890ff;
    border-radius: 4px;
    font-size: 12px;
    cursor: pointer;
    transition: all 0.2s;

    &:hover {
        background: #40a9ff;
        border-color: #40a9ff;
    }

    &:disabled {
        background: #ccc;
        border-color: #ccc;
        cursor: not-allowed;
    }
}
</style>